﻿using System.IO;
using System.Text;
using UnityEditor;
using UnityEngine;
using Yoozoo.UI.YGUI;

public class TextColorLuaCodeGenerater
{
    private static string  ColorPath = "Assets/ResourcesAssets/UI/Style/ColorStyle";
    
    [MenuItem("Tools/UI/生成TextColorStyle颜色Lua代码")]
    static void Generate()
    {

        StringBuilder luaCodeBuilder = new StringBuilder();
        luaCodeBuilder.AppendLine(@"--本代码为脚本自动生成，请不要修改");
        luaCodeBuilder.AppendLine(@"--生成脚本为TextColorLuaCodeGenerater.cs");
        luaCodeBuilder.AppendLine(@"--工具使用路径 Tools/UI/生成TextColorStyle颜色Lua代码");
        
        luaCodeBuilder.AppendLine(@"---@class TextColorCode");
        luaCodeBuilder.AppendLine(@"local TextColorCode = {}");
        string[] guids = AssetDatabase.FindAssets("t:ScriptableObject", new[] { ColorPath });
        luaCodeBuilder.AppendLine(@"TextColorCode.ColorStyle = {");
        foreach (var guid in guids)
        {
            string assetPath = AssetDatabase.GUIDToAssetPath(guid);
            ScriptableObject scriptableObject = AssetDatabase.LoadAssetAtPath<ScriptableObject>(assetPath);
            if (scriptableObject != null)
            {
                // 在这里处理ScriptableObject
                var textColorStyle = scriptableObject as YTextColorStyle;
                if (textColorStyle != null)
                {
                    luaCodeBuilder.AppendLine($"    {textColorStyle.name} = Color.New({textColorStyle.Color.r},{textColorStyle.Color.g},{textColorStyle.Color.b}),");
                }
            }
        }
        luaCodeBuilder.AppendLine(@"}");
        
        luaCodeBuilder.Append(@"return TextColorCode");
        
        string projectPath = Path.GetDirectoryName(Application.dataPath);
        // 将Lua代码保存到文件
        string filePath = projectPath + @"\Lua\common\TextColorCode.lua";
        File.WriteAllText(filePath, luaCodeBuilder.ToString());
        EditorUtility.DisplayDialog("提示", "生成成功", "确认");
    }
}
